package ca.etsmtl.log121.tp3.model;

import java.awt.Image;
import java.util.Observable;

public class PerspectiveModel extends Observable {
	public static final int ZOOMFACTOR_MULTIPLIER = 10;
	private static final int ZOOMFACTOR_DIVISOR = 100;

	private ImageModel imageModel; // Reference to the image model
	private String name;
	private int zoomFactor;
	private int posX;
	private int posY;

	public PerspectiveModel(ImageModel imageModel) {
		this.imageModel = imageModel;
		this.zoomFactor = ZOOMFACTOR_DIVISOR;
		this.posX = 0; // Maybe we should try centering the image
		this.posY = 0;
	}

	// FIXME: Finish me
	public MarkerModel save() {
		return new MarkerModel(this);
	}

	// FIXME: Finish me
	public void load(MarkerModel marker) {
		name = marker.getName();
		posX = marker.getPosX();
		posY = marker.getPosY();
		zoomFactor = marker.getZoomFactor();

		setChanged();
		notifyObservers();
	}

	public Image getImage() {
		return imageModel.getImage();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getZoomFactor() {
		return zoomFactor;
	}

	public void setZoomFactor(int zoomFactor) {
		if (zoomFactor < 10) {
			zoomFactor = 10;
		} else if (zoomFactor > 500) {
			zoomFactor = 500;
		}

		this.zoomFactor = zoomFactor;
		
		setChanged();
		notifyObservers();
	}

	public int getPosX() {
		return posX;
	}

//	public void setPosX(int posX) {
//		this.posX = posX;
//	}

	public int getPosY() {
		return posY;
	}

//	public void setPosY(int posY) {
//		this.posY = posY;
//	}

	public void setPosXPosY(int posX, int posY) {
		this.posX = posX;
		this.posY = posY;

		setChanged();
		notifyObservers();
	}

	public int getImageWidth() {
		return imageModel.getImage().getWidth(null) * getZoomFactor() / ZOOMFACTOR_DIVISOR;
	}

	public int getImageHeight() {
		return imageModel.getImage().getHeight(null) * getZoomFactor() / ZOOMFACTOR_DIVISOR;
	}
}
